export default class Utils{
    static timeList = [];
    static randomColor(a, r, g, b) {
        var col = "rgba(";
        for (var i = 0; i < 3; i++) {
            col += ((isNaN(arguments[i + 1]) || arguments[i + 1] > 255 || arguments[i + 1] < 0) ? Math.floor(Math.random() * 256) : Math.floor(arguments[i + 1])) + ",";
        }
        col += (isNaN(a) ? Math.random().toFixed(2) : a) + ")";
        return col;
    }
    static random(min, max) {
        return Math.floor(Math.random() * (max - min) + min);
    }
    static timeStart() {
        return Utils.timeList.push(new Date().getTime()) - 1;
    }
    static timeEnd(id) {
        if (Utils.timeList[id] === undefined) return 0;
        return new Date().getTime() - Utils.timeList[id];
    }
    static ce(type, style, prop, parent) {
        var div = document.createElement(type);
        if (style && typeof style === "object") Object.assign(div.style, style)
        if (prop && typeof prop === "object") Object.assign(div, prop);
        if (parent) {
            if (typeof parent === "string") parent = document.querySelector(parent);
            parent.appendChild(div);
        }
        return div;
    }
    static setCss(style) {
        if (document.styleSheets.length === 0) {
            var style = document.createElement("style");
            document.head.appendChild(style);
        }
        var styleSheet = document.styleSheets[document.styleSheets.length - 1];
        for (var prop in style) {
            var css = Object.keys(style[prop]).reduce(function (value, item) {
                var v = style[prop][item];
                if (["width", "height", "left", "top", "fontSize"].includes(item) && !String(v).includes("px")) {
                    v += "px";
                }
                return value + item + ":" + v + ";"
            }, "");
            css = css.replace(/([A-Z])/g, function ($1) {
                return "-" + $1.toLowerCase();
            });
            styleSheet.addRule(prop, css, styleSheet.cssRules.length);
        }
    }
    static addCss(css){
        if (document.styleSheets.length === 0) {
            var style = document.createElement("style");
            document.head.appendChild(style);
        }
        var styleSheet = document.styleSheets[document.styleSheets.length - 1];
        css.replace(/\r|\n/g,"").replace(/(.*?)\{(.*?)\}/g,function(item,a,b){
            styleSheet.addRule(a, b, styleSheet.cssRules.length);
        })
 
    }
    static loadImage(srcList, callback, basePath = "", extension = "jpg") {
        var img = new Image();
        img.index = 0;
        img.list = [];
        var extens = ["jpg", "jpeg", "png", "gif", "bmp", "webp"];
        img.srcList = srcList.map(function (item) {
            basePath = basePath.slice(-1) === "/" ? basePath : basePath + "/";
            var itemName = item.split(".").pop();
            return basePath + (extens.indexOf(itemName) > -1 ? item : item + "." + extension);
        })
        img.callback = callback;
        img.obj = this;
        img.addEventListener("load", this.loadHanlder);
        img.src = img.srcList[img.index];
    }
    static loadHanlder(e) {
        this.list.push(this.cloneNode(false));
        this.index++;
        if (this.index === this.srcList.length) {
            this.removeEventListener("load", this.obj.loadHanlder)
            this.callback(this.list);
            return
        }
        this.src = this.srcList[this.index];
    }
    static dragOn(elem,clock) {
        document.obj=this;
        elem.clock=clock;
        elem.w=elem.offsetWidth;
        elem.h=elem.offsetHeight;
        elem.addEventListener("mousedown", this.mouseHandler);
    }
    static dragOff(elem) {
        elem.removeEventListener("mousedown", this.mouseHandler);
    }
    static  mouseHandler(e) {
        if (e.type === "mousedown") {
            e.preventDefault();
            document.offsetX = e.offsetX;
            document.offsetY = e.offsetY;
            document.currentTarget = this;
            document.addEventListener("mousemove",document.obj.mouseHandler);
            document.addEventListener("mouseup", document.obj.mouseHandler);
        } else if (e.type === "mousemove") {
            var rect=this.currentTarget.parentElement.getBoundingClientRect();
            var x=e.clientX - this.offsetX-rect.x;
            var y= e.clientY - this.offsetY-rect.y;
            if(this.currentTarget.clock){
                if(x<0) x=0;
                if(y<0) y=0;
                if(x>rect.width-this.currentTarget.w) x=rect.width-this.currentTarget.w
                if(y>rect.height-this.currentTarget.h) y=rect.height-this.currentTarget.h
            }
            this.currentTarget.style.left =  x+ "px";
            this.currentTarget.style.top = y+ "px";
        } else if (e.type === "mouseup") {
            document.removeEventListener("mousemove", document.obj.mouseHandler);
            document.removeEventListener("mouseup", document.obj.mouseHandler);
        }
    }
}
